package com.ld.control.business.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.user.mapper.RoleMapper;
import com.ld.control.business.user.mapper.RoleMenuMapper;
import com.ld.control.business.user.model.RoleMenuModel;
import com.ld.control.business.user.model.RoleModel;
import com.ld.control.business.user.service.RoleService;
import com.ld.control.business.user.vo.req.RoleReqVo;
import com.ld.control.business.user.vo.resp.RoleRespVo;
import com.ld.control.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-04-12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleModel> implements RoleService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Override
    public List<RoleRespVo> getList() {
        return roleMapper.selectAllList();
    }

    @Override
    public RoleRespVo getById(Long id) {
        RoleRespVo roleRespVo = new RoleRespVo();
        RoleModel roleModel = roleMapper.selectById(id);
        BeanUtils.copyProperties(roleModel, roleRespVo);

        QueryWrapper<RoleMenuModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RoleMenuModel.ROLE_ID, id);
        List<RoleMenuModel> roleMenuModelList = roleMenuMapper.selectList(queryWrapper);
        List<Long> menuIdList = roleMenuModelList.stream().map(RoleMenuModel::getMenuId).toList();
        roleRespVo.setMenuIdList(menuIdList);
        return roleRespVo;
    }

    @Override
    public void update(RoleReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        // 修改角色名称
        RoleModel roleModel = new RoleModel();
        BeanUtils.copyProperties(reqVo, roleModel);
        roleModel.setUpdateBy(currentUserAccountId);
        roleModel.setUpdateTime(new Date());
        roleMapper.updateById(roleModel);
        // 清除原来角色关联的菜单数据
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put(RoleMenuModel.ROLE_ID, roleModel.getId());
        roleMenuMapper.deleteByMap(columnMap);
        // 新增新的菜单信息
        addRoleMenu(reqVo.getId(), reqVo.getMenuIdList());
    }

    @Override
    public void add(RoleReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        RoleModel roleModel = new RoleModel();
        BeanUtils.copyProperties(reqVo, roleModel);
        roleModel.setCreateBy(currentUserAccountId);
        roleModel.setCreateTime(new Date());
        roleMapper.insert(roleModel);
        // 新增新的菜单信息
        addRoleMenu(roleModel.getId(), reqVo.getMenuIdList());
    }

    /**
     * 新增角色和菜单关联关系
     * @param roleId
     * @param menuIdList
     */
    private void addRoleMenu(Long roleId, List<Long> menuIdList) {
        if (!CollectionUtils.isEmpty(menuIdList)) {
            RoleMenuModel roleMenuModel;
            for (Long menuId : menuIdList) {
                roleMenuModel = new RoleMenuModel();
                roleMenuModel.setRoleId(roleId);
                roleMenuModel.setMenuId(menuId);
                roleMenuMapper.insert(roleMenuModel);
            }
        }
    }

}
